/**
 * @class   wosh::building::AudioBox
 * @brief   BuildingObject representing a generic (real) audio box.
 *          This class provides standard Methods and properties to 
 *          control the the mapped wosh::interfaces::devices::PlayerAudio
 *          wosh::building::AudioBox is usually a child of wosh::building::Room
 *
 ****************************************************************************
 * @version $Id: AudioBox.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Building_AudioBox_H__
 #define __WOSH_Building_AudioBox_H__

 #include <framework/building/BuildingObject.h>

 #include <interfaces/devices/PlayerAudio.h>


namespace wosh {
 namespace building {

 #define _AudioBox_VERSION					0.27

 #define _AudioBox_KEY_volume				"volume"
 #define _AudioBox_KEY_Device				"Device"

 #define _AudioBox_METHOD_play				"play"
 #define _AudioBox_METHOD_enqueue			"enqueue"
 #define _AudioBox_METHOD_stop				"stop"
 #define _AudioBox_METHOD_synch				"synch"

 #define _AudioBox_METHOD_mute				"mute"
 #define _AudioBox_METHOD_volume			"volume"
 #define _AudioBox_METHOD_volume_up			"volume_up"
 #define _AudioBox_METHOD_volume_down		"volume_down"


class AudioBox : public BuildingObject
 {
	WOSH_BUILDINGOBJECT(wosh::building::AudioBox)

	public:
		/**
		 * @brief   audiobox states
		 */
		enum AUDIOBOX_STATE {
			AUDIOBOX_UNKNOWN	= 0x00,	///< audiobox state is unknown
			AUDIOBOX_IDLE		= 0x01,	///< audiobox is idle (no output)
			AUDIOBOX_PLAYING	= 0x02	///< audiobox is playing (some output)
		};

	public:
		/**
		 * @brief  Empty constructor.
		 */
		AudioBox();
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		AudioBox( const AudioBox& m ) : BuildingObject(m) { }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~AudioBox() { }


	public:
		virtual void busMessageChild( const Message& message );

		virtual WRESULT setChild( const std::string& object_uri, InterfacesProvider* interfaces );

//@}
/** @name AudioBox Interface - 
 * @{
 ******************************************************************************/
	public:
		virtual WRESULT play( const std::string& resource, const SecurityToken* security = NULL );
		virtual WRESULT enqueue( const std::string& resource, const SecurityToken* security = NULL );

		virtual WRESULT stop( const SecurityToken* security = NULL );

		virtual WRESULT synch( const std::string& target_audiobox, const SecurityToken* security = NULL );

		virtual WRESULT setMuted( bool muted, const SecurityToken* security = NULL );

		virtual WRESULT setVolume( int volume, const SecurityToken* security = NULL );

		virtual WRESULT setVolumeUp()					{ return mmDoSetVolumeUp(NULL);}
		virtual WRESULT setVolumeDown()					{ return mmDoSetVolumeDown(NULL);}

	public:
		virtual std::string getPlayerAudioName() const	{ return getChildByInterface( _PlayerAudio_TYPE ); }

		/**
		 * @brief   Get last known audio-box state (opened, closed, unknown)
		 * @return  current audio-box state (enum)
		 */
		AudioBox::AUDIOBOX_STATE getState() const		{ return this->state; }
		/**
		 * @brief   Get audio-box state as a literal string
		 * @return  current audio-box state as literal string
		 */
		inline const char* getStateAsString() const		{ return getStateAsString(this->state); }
		/**
		 * @brief   Get audio-box state as a literal string
		 * @param state [in] audio-box state to be retrieved as literal string
		 * @return  passed audio-box state as literal string
		 */
		static const char* getStateAsString( AudioBox::AUDIOBOX_STATE state );
		/**
		 * @brief   Get audio-box state from a literal string
		 * @param state [in] audio-box state as literal string
		 * @return  audio-box state (as enum) associated with the argument 
		 */
		static AudioBox::AUDIOBOX_STATE getStateFromString( const char* state );

//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual bool updateHash( DataHashContext* hash_context ) const;
//@}
	private:
		Response* mmDoPlay( const Message* messageRequest );
		Response* mmDoEnqueue( const Message* messageRequest );
		
		Response* mmDoStop( const Message* messageRequest );

		Response* mmDoSynch( const Message* messageRequest );

		Response* mmDoSetVolume( const Message* messageRequest );
		Response* mmDoMute( const Message* messageRequest );

		WRESULT mmDoSetVolumeUp( const SecurityToken* security );
		WRESULT mmDoSetVolumeDown( const SecurityToken* security );

	protected:
		virtual void init();
		void setState( AudioBox::AUDIOBOX_STATE state, bool raiseEvent = true, const SecurityToken* source_token = NULL );

	protected:
		AudioBox::AUDIOBOX_STATE state;	///< current state of the audiobox


}; // class def




/**
 * @class   wosh::building::AudioBox_StateChanged
 * @brief   Raised when door state is changed (such as open to closed).
 *
 * Internally, data is stored as wosh::List. Supports also wosh::Variant type.
 *
 * Fields are:
 *  - \c audiobox_state [Variant::INTEGER] (always present, first item of the list)
 *  - \c previous_appliance_state (optional, second item of the list)
 *
 * Some getters are provided for convenience:
 * \code

 * \endcode
 *
 * @see     wosh::Fact
 * @see     wosh::building:AudioBox
 ****************************************************************************
 * @version 0.8.399 $Id: AudioBox.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
class AudioBox_StateChanged : public wosh::Fact
 {
	WOSH_SERIALIZABLE(wosh::building::AudioBox_StateChanged)

	public:
		/**
		 * @brief  Empty constructor.
		 */
		AudioBox_StateChanged()
			: wosh::Fact(_BuildingObject_EVENT_StateChanged) { }
		/**
		 * @brief  Constructor with initializers.
		 * @param current_audiobox_state [in] current state of the appliance
		 */
		AudioBox_StateChanged( AudioBox::AUDIOBOX_STATE current_audiobox_state );
		/**
		 * @brief  Constructor with initializers.
		 * @param current_audiobox_state [in] current state of the appliance
		 * @param current_volume [in] current level of the appliance
		 */
		AudioBox_StateChanged( AudioBox::AUDIOBOX_STATE current_audiobox_state, int current_volume );
		/**
		 * @brief  Constructor with initializers.
		 * @param current_audiobox_state [in] current state of the appliance
		 * @param current_volume [in] current level of the appliance
		 * @param previous_appliance_state [in] previous state of the appliance
		 * @param previous_volume [in] previous level of the appliance
		 */
		AudioBox_StateChanged( AudioBox::AUDIOBOX_STATE current_audiobox_state, int current_volume, AudioBox::AUDIOBOX_STATE previous_appliance_state, int previous_volume );
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		AudioBox_StateChanged( const AudioBox_StateChanged& m )
			: wosh::Fact(m) { }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~AudioBox_StateChanged() { }

	public:
		/**
		 * @brief   Get appliance state (opened, closed, unknown).
		 * @return  current appliance state (enum)
		 */
		AudioBox::AUDIOBOX_STATE getAudioBoxState() const;
		/**
		 * @brief   Get appliance level.
		 * @return  current appliance level (value)
		 */
		int getVolume() const;

		/**
		 * @brief   Get previous appliance state (opened, closed, unknown).
		 * @return  previous appliance state (enum)
		 */
		AudioBox::AUDIOBOX_STATE getPreviousAudioBoxState() const;
		/**
		 * @brief   Get appliance level.
		 * @return  current appliance level (value)
		 */
		int getPreviousVolume() const;

		inline bool isOn() const				{ return (getVolume() > 0); }
		inline bool isOff() const				{ return (getVolume() == 0); }

}; // class def





 }; // namespace building
}; // namespace wosh

#endif //__WOSH_Building_AudioBox_H__
